package com.ykt.controller;

import com.jfinal.aop.Before;
import com.ykt.model.Address;
import com.ykt.service.AddressService;
import com.ykt.validator.SysAddressValidator;
import com.ykt.vo.AddressVo;
import com.ykt.vo.ResultVo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author long.hua
 * @version 1.0.0
 * @since 2016-01-30 13:22
 */
public class SysAddressController extends BaseController {

    private AddressService addressService = enhance(AddressService.class.getName(), AddressService.class);


    /**
     * 查询所有的地址信息
     */
    public void findAll() {
        int clientVersion = getParaToInt("version", -1); // 当前版本
        int serverVersion = addressService.version(); // 最新版本
        if (serverVersion <= clientVersion) {
            returnJson(ResultVo.success());
            return;
        }

        List<Address> addresses = addressService.findAll();

        // 服务器端版本有更新则发送全部数据
        List<AddressVo> vos = new ArrayList<>();
        for (Address entity : addresses) {
            if (entity.getParentId() == 0) {
                AddressVo obj = new AddressVo();
                obj.setId(entity.getId());
                obj.setParentId(entity.getParentId());
                obj.setName(entity.getName());
                vos.add(obj);
            }
        }

        // 格式化为树形结构
        List<AddressVo> addressVos = recursiveTree(addresses, vos);

        // 组装结果为规定格式
        Map<String, Object> addressMap = new HashMap<>();
        addressMap.put("version", serverVersion); // 固定key名
        addressMap.put("address", addressVos); // 固定key名

        // 返回结果
        returnJson(ResultVo.success(addressMap));
    }

    /**
     * 查询指定ID的地址信息，客户端可调可不调
     */
    @Before(SysAddressValidator.class)
    public void findByIds() {
        List<Integer> addressIds = new ArrayList<>();

        String ids = getPara("ids");
        String arr[] = ids.split(",");
        for (String item : arr) {
            addressIds.add(Integer.valueOf(item));
        }

        Map<Integer, String> addressMap = addressService.findByIds(addressIds);
        returnJson(addressMap);
    }

    /**
     * 根据父级ID查询子级地址
     */
    public void findSub() {
        int id = getParaToInt("id", -1);
        List<Address> addresses = addressService.findByParentId(id);
        returnJson(ResultVo.success(addresses));
    }

    // 递归组装树
    private List<AddressVo> recursiveTree(List<Address> allNodes, List<AddressVo> topNodes) {
        for (AddressVo parentVo : topNodes) {
            List<AddressVo> children = new ArrayList<>();

            int id = parentVo.getId();
            for (Address entity : allNodes) {
                if (id == entity.getParentId()) {

                    AddressVo childVo = new AddressVo();
                    childVo.setId(entity.getId());
                    childVo.setParentId(entity.getParentId());
                    childVo.setName(entity.getName());

                    children.add(childVo);
                }
            }

            parentVo.setChildren(children); // 设置子级

            recursiveTree(allNodes, parentVo.getChildren());
        }

        return topNodes;
    }

}
